//
//  TIMNewFriendVC.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/30.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMNewFriendVC.h"
#import "TIMNewFriendCell.h"
#import "TIMDemoMacro.h"
#import "TIMAddFriendVC.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"
@interface TIMNewFriendVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *friendTableView; //!<
@property (nonatomic,strong) NSMutableArray *friendDataSource;//!<

@end

@implementation TIMNewFriendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.friendDataSource = [[[TIMStorageManager sharedStorageManager] getAllNewFriend] mutableCopy];
    [self setUpNav];

    [self.view addSubview:self.friendTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receicveFriend:) name:TIM_OPERATE_FRIEND_ACTION_BROADCAST object:nil];
}

- (void)receicveFriend:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    switch ([userInfo[@"TIMMessageFriendOperation"] integerValue]) {
        case TIMMsgOperation_OperationFriAdd:
            NSLog(@"好友操作： TIMMsgOperation_OperationFriAdd");
        case TIMMsgOperation_OperationFriConfirm:
            NSLog(@"好友操作： TIMMsgOperation_OperationFriConfirm");
            break;
        default:
            break;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.friendDataSource = [[[TIMStorageManager sharedStorageManager] getAllNewFriend] mutableCopy];
        [self.friendTableView reloadData];
    });
}

- (void)setUpNav {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 60, 30);
    leftBtn.titleLabel.font = TPSFontSize(15);
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];;
    self.navigationItem.title = @"新的朋友";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 65, 25);
    btn.titleLabel.font = TPSFontSize(15);
    [btn setTitle:@"添加新朋友" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addFriendBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addFriendBtnClicked {
    TIMAddFriendVC *vc = [[TIMAddFriendVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendDataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"friendCellID";
    TIMNewFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    TIMUserInfo *model = self.friendDataSource[indexPath.row];
    cell.friendmodel = model;
    [cell.accessBtn addTarget:self action:@selector(accessBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)accessBtn:(UIButton *)button {
    TIMNewFriendCell *cell = (TIMNewFriendCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.friendTableView indexPathForCell:cell];
     TIMUserInfo *model = self.friendDataSource[indexPath.row];
    TIMUserInfo *currentUser = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
    [TIMApi addFriendWithSenderId:currentUser.userID ReceiverId:model.userID MsgMemo:model.remarkName MsgGroup:[@"fenzu" dataUsingEncoding:NSUTF8StringEncoding] MsgTip:[@"beizhu" dataUsingEncoding:NSUTF8StringEncoding] MsgOperation:TIMMsgOperation_OperationFriConfirm completion:^(NSError *error) {
        if (!error) {
            NSLog(@"确认成功");
            dispatch_async(dispatch_get_main_queue(), ^{
                self.friendDataSource = nil;
                [self.friendTableView reloadData];
            });
        }
    }];
}

#pragma mark - Get

- (UITableView *)friendTableView {
    if (!_friendTableView) {
        _friendTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 64) style:UITableViewStylePlain];
        _friendTableView.backgroundColor = [UIColor whiteColor];
        _friendTableView.delegate = self;
        _friendTableView.dataSource = self;
        _friendTableView.rowHeight = 60;
        _friendTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_friendTableView registerClass:[TIMNewFriendCell class] forCellReuseIdentifier:@"friendCellID"];
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 10)];
        v.backgroundColor = [UIColor clearColor];
        _friendTableView.tableHeaderView = v;
        //cell无数据时，不显示间隔线
        UIView *vc = [[UIView alloc] initWithFrame:CGRectZero];
        vc.backgroundColor = [UIColor clearColor];
        _friendTableView.tableFooterView = vc;
    }
    return _friendTableView;
}

@end
